# Maintainer: Felix Yan <felixonmars@archlinux.org>

pkgbase=volume_key
pkgname=(volume_key python-volume_key)
pkgver=0.3.12
pkgrel=8
pkgdesc="A library for manipulating storage volume encryption keys and storing them separately from volumes to handle forgotten passphrases"
arch=('x86_64')
url="https://pagure.io/volume_key"
license=('GPL2')
makedepends=(
  'cryptsetup'
  'glib2'
  'glibc'
  'gpgme'
  'nspr'
  'nss'
  'python'
  'swig'
  'util-linux-libs'
)
source=("https://releases.pagure.org/volume_key/volume_key-$pkgver.tar.xz")
sha512sums=('d056154c9b9d23e4eb661946dd59ed97e116903a3afcff9d9e29258408082f33dcbb69958724143f6bf191a3da488a03b6c02af287790990ed6459e29d66553c')
b2sums=('4a0424d51878855c69bdf969b616f3a563074976ad7b07aa848140266d97f66de9c52e1a6ed8913df93077d5966c3ab99afc78c3b22c410f1eadf5447ca2f416')

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

prepare() {
  cd $pkgname-$pkgver
  autoreconf -fiv
  find . -name ".pyc" -delete
}

build() {
  cd $pkgname-$pkgver
  export CFLAGS+=" $(python3-config --includes)"
  ./configure --prefix=/usr
  # prevent excessive overlinking due to libtool
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make
}

package_volume_key() {
  depends=(
    'cryptsetup'
    'glib2'
    'glibc'
    'gpgme'
    'nspr'
    'nss'
    'util-linux-libs'
  )
  optdepends=(
    'python-volume_key: for Python bindings'
  )

  make DESTDIR="$pkgdir" install -C $pkgname-$pkgver

  (
    cd "$pkgdir"
    _pick python-$pkgbase usr/lib/python*
  )
}

package_python-volume_key() {
  pkgdesc+=" - Python bindings"
  depends=(
    'glib2'
    'glibc'
    'nss'
    'python'
    "volume_key=$pkgver"
  )

  mv -v $pkgname/* "$pkgdir"
}
